

package org.come.Jpanel;

import com.tool.Stall.Commodity;
import com.tool.Stall.Stall;
import com.tool.Stall.StallBean;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.StallBuyBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.come.mouslisten.CancelStallMouslisten;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.Util;

 

public class StallBuyJpanel extends JPanel {
    private int id;
    private String type = "金币";
    private Stall stall;
    private JTextField textNumber;
    private String Boothname;
    private BigDecimal GoodsPrice;
    private BigDecimal GoodsTotalPrice;
    private StallBuyBtn btnBuy;
    public JLabel[] GoodsListLabel = new JLabel[24];
    public String[] Usetime = new String[24];
    public JLabel[] PetsListLabel = new JLabel[5];
    final ImageIcon imageIcon = new ImageIcon("img/123_副本.png");
    ImageIcon icon;

    public StallBuyJpanel() throws Exception {
    		this.setPreferredSize(new Dimension(418, 489));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            GoodsPrice = new BigDecimal(0);
            GoodsTotalPrice = new BigDecimal(0);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 35);
            offBtn.setBounds(418 - 37, 10, 25, 25);
            this.add(offBtn);
            // 放置出售的物品数量
            textNumber = new JTextField() {
                Image image = imageIcon.getImage();
                Image grayImage = GrayFilter.createDisabledImage(image);
                {
                    setOpaque(false);
                } // instance initializer

                @Override
                public void paint(Graphics g) {
                    // 背景图片
                    g.drawImage(grayImage, 0, 0, this);
                    super.paint(g);
                }
            };
            textNumber.setBounds(90, 396, 153, 16);
            textNumber.setText("0");
            textNumber.setForeground(Color.WHITE);
            textNumber.setFont(new Font("微软雅黑", 0, 14));
            textNumber.setBackground(UIUtils.Color_BACK);
            textNumber.setCaretColor(Color.white);
            textNumber.setBorder(BorderFactory.createEmptyBorder());
            textNumber.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void removeUpdate(DocumentEvent e) {
                    // TODO Auto-generated method stub
                    String s = textNumber.getText();
                    try {
                        if (btnBuy.getCommodity() != null) {
                            if (btnBuy.getCommodity().getGood() != null) {
                                BigDecimal number = new BigDecimal(s);
                                GoodsTotalPrice = (GoodsPrice.multiply(number));
                            } else if (btnBuy.getCommodity().getPet() != null) {
                                GoodsTotalPrice = GoodsPrice;
                            }
                        } else {
                            GoodsTotalPrice = new BigDecimal(0);
                            GoodsPrice = GoodsTotalPrice;
                        }
                    } catch (NumberFormatException e1) {
                    }
                }

                @Override
                public void insertUpdate(DocumentEvent e) {
                    // TODO Auto-generated method stub
                    String s = textNumber.getText();
                    try {
                        if (btnBuy.getCommodity() != null) {
                            if (btnBuy.getCommodity().getGood() != null) {
                                BigDecimal number = new BigDecimal(s);
                                GoodsTotalPrice = (GoodsPrice.multiply(number));
                            } else if (btnBuy.getCommodity().getPet() != null) {
                                GoodsTotalPrice = GoodsPrice;
                            }
                        } else {
                            GoodsTotalPrice = new BigDecimal(0);
                            GoodsPrice = GoodsTotalPrice;
                        }
                    } catch (NumberFormatException e1) {
                    }
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            textNumber.addKeyListener(new KeyListener() {
                // 键盘监听
                @Override
                public void keyTyped(KeyEvent e) {
                    // 让该输入框只能输入数字
                    int keyChar = e.getKeyChar();
                    if (!(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)) {
                        e.consume();
                        return;
                    }
                    if (btnBuy.getCommodity() == null) {
                        e.consume();
                        return;
                    }
                    if (btnBuy.getCommodity().getPet() != null) {
                        e.consume();
                        return;
                    }
                    if (btnBuy.getCommodity().getGood() != null) {
                        String key = textNumber.getText();
                        String value = getLimit(key, btnBuy.getCommodity().getGood().getUsetime()); // 该框只能输入这个物品的最大数量
                        if (value.indexOf(e.getKeyChar()) < 0) {
                            e.consume();
                        }
                    }
                }

                @Override
                public void keyReleased(KeyEvent e) {
                }

                @Override
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            this.add(textNumber);
            int Flag = 0;
            int count = 1;
            for (int i = 0; i < 24; i++) {
                Flag = i % 6;
                count = i / 6;
                GoodsListLabel[i] = new JLabel();
                GoodsListLabel[i].addMouseListener(new CancelStallMouslisten(i, 0, this));
                GoodsListLabel[i].setBounds(48 + Flag * 51, 71 + 51 * count, 48, 48);
                this.add(GoodsListLabel[i]);
            }
            // 存放宠物头像的五个表格框
            for (int j = 0; j < 5; j++) {
                PetsListLabel[j] = new JLabel();
                PetsListLabel[j].addMouseListener(new CancelStallMouslisten(j, 1, this));
                PetsListLabel[j].setBounds(50 + j * 63, 305, 50, 51);
                this.add(PetsListLabel[j]);
            }
            // 购买
            btnBuy = new StallBuyBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "购买", this);
            btnBuy.setBounds(290, 407, 59, 24);
            this.add(btnBuy);
    }

    @Override
    protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B249.png");
            }
            // 道具栏
            g.drawImage(icon.getImage(), 0, 0, 418, 489, this);
            g.setFont(UIUtils.TEXT_MSG);
            g.setColor(Color.CYAN);
            if (Boothname != null) {
                g.drawString(Boothname, 100, 33 + 13);
            }
            g.setColor(Color.white);
            for (int i = 0; i < Usetime.length; i++) {
                int row = i % 6;
                int col = i / 6;
                // 判断如果数量值不为空
                if (Usetime[i] != null) {
                    g.drawString(Usetime[i], 48 + row * 51, 71 + 13 + col * 51);
                }
            }
            Util.drawMoney(g, 90, 446 + 13);
            Util.drawPrice(g, GoodsPrice, 90, 371 + 13);
            Util.drawPrice(g, GoodsTotalPrice, 90, 421 + 13);
    }

    public JTextField getTextNumber() {
        return this.textNumber;
    }

    public void setTextNumber(JTextField textNumber) {
        this.textNumber = textNumber;
    }

    public BigDecimal getGoodsPrice() {
        return this.GoodsPrice;
    }

    public void setGoodsPrice(BigDecimal goodsPrice) {
        this.GoodsPrice = goodsPrice;
    }

    public BigDecimal getGoodsTotalPrice() {
        return this.GoodsTotalPrice;
    }

    public void setGoodsTotalPrice(BigDecimal goodsTotalPrice) {
        this.GoodsTotalPrice = goodsTotalPrice;
    }

    public void reset(Commodity commodity) {
        this.btnBuy.setCommodity(commodity);
        this.textNumber.setText("1");
        this.GoodsPrice = new BigDecimal(commodity.getMoney());
        this.GoodsTotalPrice = this.GoodsPrice;
    }

    public void no(StallBean bean) {
        if (this.id == bean.getId()) {
            int i;
            for(i = 0; i < this.GoodsListLabel.length; ++i) {
                this.GoodsListLabel[i].setIcon((Icon)null);
                this.Usetime[i] = null;
            }

            for(i = 0; i < this.PetsListLabel.length; ++i) {
                this.PetsListLabel[i].setIcon((Icon)null);
            }

            this.Boothname = "";
            FormsManagement.HideForm(35);
        }

    }

    public void initstall(Stall stall) {
        this.id = stall.getId();
        this.stall = stall;
        this.Boothname = stall.getStall();

        int i;
        Commodity commodity;
        ImageIcon img;
        for(i = 0; i < stall.getGoodstables().length; ++i) {
            commodity = stall.getGoodstables()[i];
            if (commodity == null) {
                this.GoodsListLabel[i].setIcon((Icon)null);
                this.Usetime[i] = null;
            } else {
                img = GoodsListFromServerUntil.imgpathAdaptive(commodity.getGood().getSkin(), 49, 49);
                this.GoodsListLabel[i].setIcon(img);
                this.Usetime[i] = commodity.getGood().getUsetime().toString();
            }
        }

        for(i = 0; i < stall.getPets().length; ++i) {
            commodity = stall.getPets()[i];
            if (commodity == null) {
                this.PetsListLabel[i].setIcon((Icon)null);
            } else {
                img = new ImageIcon("inkImg/hongmu/2/101_png.xy2uiimg.pet_def.png");
                img.setImage(img.getImage().getScaledInstance(49, 49, 10));
                this.PetsListLabel[i].setIcon(img);
            }
        }

    }

    private String getLimit(String key, int num) {
        String value = String.valueOf(num);
        int[] a = new int[13];
        int b = num;
        int len = key.length();
        int length = value.length();

        int in;
        for(in = 0; b > 0; b /= 10) {
            a[length - 1 - in] = b % 10;
            ++in;
        }

        if ("".equals(key)) {
            in = 0;
        } else {
            in = Integer.valueOf(key) * 10;
        }

        if (len < length - 1) {
            if (len == 0) {
                value = string(1, 9);
            } else {
                value = string(0, 9);
            }
        } else if (len == length - 1) {
            if (in <= num) {
                if (in == 0) {
                    value = string(1, a[len]);
                } else {
                    value = string(0, a[len]);
                }
            } else {
                value = "";
            }
        } else {
            value = "";
        }

        return value;
    }

    private static String string(int a, int b) {
        String value = "";

        for(int i = a; i <= b; ++i) {
            value = value + String.valueOf(i);
        }

        return value;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Stall getStall() {
        return this.stall;
    }

    public void setStall(Stall stall) {
        this.stall = stall;
    }
}
